﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using BaseControls;
using HOG.Controls;

namespace HOG.Controls
{
    /// <summary>
    /// PauseableBehavior is a trigger that is raised when user is click on pause button.
    /// </summary>
    public class PauseBehavior : TriggerAction<UIElement>
    {        
        private static bool isShow = false;
        public static bool canPause = true;                                
        private void ClosePausableControl()
        {            
            UIElement control = LogicalTreeHelper.FindName("FrameControl", (FrameworkElement)AssociatedObject);
            if (control != null)
                (control as IHudGameInterface).GetClock().UnPause();
            Canvas rootControl = (Canvas)LogicalTreeHelper.FindName("RootControl", (FrameworkElement)AssociatedObject);
            if (rootControl != null)
                foreach(UIElement item in rootControl.Children)
                    //if (item is PausableControl)
                    if (item is OptionsControl)
                    {
                        rootControl.Children.Remove(item);                        
                        break;
                    }
            isShow = false;
        }
        private void ShowPauseControl()
        {
            if (!isShow)
                FillSpace(AssociatedObject);
            else
                ClosePausableControl();
        }      
        private void FillSpace(UIElement parent)
        {
            isShow = true;
            FrameControl control = (FrameControl)LogicalTreeHelper.FindName("FrameControl", (FrameworkElement)AssociatedObject);
            if (control != null)
                (control as IHudGameInterface).GetClock().Pause();
            Canvas rootControl = (Canvas)LogicalTreeHelper.FindName("RootControl", (FrameworkElement)AssociatedObject);
            if (rootControl != null)
            {                
                OptionsControl pause = new OptionsControl(control);
                pause.SetValues(control.brightness.Brightness,control.contrast.Contrast,control.gamma.Gamma);
                pause.Close = new EventHandler((obj, ev) => { isShow = false; });
                rootControl.Children.Add(pause);
            }
        }
        protected override void Invoke(object parameter)
        {
            if (!canPause)
                return;
            ShowPauseControl();
        }
    }
}